<!doctype html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <meta name="renderer" content="webkit|ie-comp|ie-stand">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta http-equiv="Cache-Control" content="no-siteapp"/>
    <link rel="shortcut icon" th:href="@{../../favicon.ico}" type="image/x-icon"/>
    <link rel="stylesheet" th:href="@{../../assets/css/font.css}">
    <link rel="stylesheet" th:href="@{../../assets/css/xadmin.css}">
    <link rel="stylesheet" th:href="@{../../assets/lib/toast/css/toast.style.css}">
    <script type="text/javascript" src="https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js" charset="utf-8"></script>
    <script type="text/javascript" th:src="@{../../assets/lib/layui/layui.js}" charset="utf-8"></script>
    <script type="text/javascript" th:src="@{../../assets/js/xadmin.js}" charset="utf-8"></script>
    <script type="text/javascript" th:src="@{../../assets/lib/toast/js/toast.script.js}" charset="utf-8"></script>
    <script type="text/javascript" th:src="@{../../assets/lib/date/dateformat.js}" charset="utf-8"></script>
    <title>管理员-X-admin2.0</title>

</head>
<body>
<div class="x-body">

   <input type="hidden" id="userId" name="userId" th:value="${userId}">

    <form class="layui-form">
        <div class="layui-form-item">
            <label class="layui-form-label">角色</label>
            <div class="layui-input-block">
                <th:block th:each="role:${systemRoleList}">
                    <th:block th:if="${role.checkbox eq ''}">
                        <input type="checkbox" th:title="${role.roleName}" name="roleId" th:value="${role.roleId}">
                    </th:block>
                    <th:block th:if="${role.checkbox eq 'checked'}">
                        <input type="checkbox" th:title="${role.roleName}" name="roleId" th:value="${role.roleId}" th:checked="${role.checkbox}">
                    </th:block>
                </th:block>
            </div>
        </div>
        <br/>
        <div class="layui-form-item">
            <label class="layui-form-label">
            </label>
            <button type="submit" class="layui-btn layui-btn-fluid" lay-filter="*" lay-submit>
                提交
            </button>
        </div>
    </form>
</div>
<script>
    layui.use(['laydate', 'table', 'form'], function () {

        $ = layui.jquery;
        var laydate = layui.laydate, table = layui.table, form = layui.form;

        form.on('submit(*)', function (data) {
            var userId=$(" input[ id='userId' ] ").val()
            var vals = [];
            $('input:checkbox:checked').each(function (index, item) {
                vals.push($(this).val());
            });
            console.log(vals.toString());
            $.ajax({
                url: "/system/user/user-setupRole.json",
                type: "POST",
                data: {
                    roleIds:vals.toString(),
                    userId:userId
                },
                success: function (result) {
                    if (result.status) {
                        var index = parent.layer.getFrameIndex(window.name); //获取窗口索引
                        layer.msg("操作成功");
                        parent.layer.close(index);  // 关闭layer
                        window.parent.location.reload(); //刷新父页面
                    } else {
                        layer.msg(result.msg);
                    }
                },
                error: function () {
                    layer.msg("修改错误，请联系开发人员进行排查");
                }
            });
            return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
        });

    });
</script>
</body>

</html>